package com.easylinkin.linkappapi.space.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easylinkin.linkappapi.annotation.CommonOperateLogAnnotate;
import com.easylinkin.linkappapi.asset.entity.Asset;
import com.easylinkin.linkappapi.asset.service.AssetService;
import com.easylinkin.linkappapi.calculaterule.mapper.CalculateConfigMapper;
import com.easylinkin.linkappapi.device.entity.Device;
import com.easylinkin.linkappapi.device.entity.DeviceRefAreaScope;
import com.easylinkin.linkappapi.device.service.DeviceRefAreaScopeService;
import com.easylinkin.linkappapi.device.service.DeviceService;
import com.easylinkin.linkappapi.operatelog.constant.LogConstant.LogModule;
import com.easylinkin.linkappapi.security.context.LinkappUserContextProducer;
import com.easylinkin.linkappapi.space.entity.LinkappSpace;
import com.easylinkin.linkappapi.space.service.LinkappSpaceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import site.morn.rest.RestBuilders;
import site.morn.rest.RestMessage;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author liuming
 * @since 2020-04-14
 */
@RestController
@RequestMapping("/space")
@Api(value="空间控制器",tags={"空间"})
public class LinkappSpaceController {

    @Autowired
    LinkappSpaceService linkappSpaceService;
    @Autowired
    DeviceService deviceService;
    @Autowired
    AssetService assetService;
    @Lazy
    @Resource
    DeviceRefAreaScopeService deviceRefAreaScopeService;
    @Resource
    LinkappUserContextProducer linkappUserContextProducer;

    @Resource
    private CalculateConfigMapper calculateConfigMapper;

    @ApiOperation("新增空间")
    @CommonOperateLogAnnotate(module = LogModule.SPACE, desc = "新增空间")
    @PostMapping("/addLinkappSpace")
    public RestMessage addLinkappSpace(@RequestBody LinkappSpace linkappSpace) {
        linkappSpaceService.addLinkappSpace(linkappSpace);
        return RestBuilders.successBuilder().data(linkappSpace).build();
    }

    @ApiOperation("修改空间")
    @CommonOperateLogAnnotate(module = LogModule.SPACE, desc = "修改空间")
    @PostMapping("/updateLinkappSpace")
    public RestMessage updateLinkappSpace(@RequestBody LinkappSpace linkappSpace) {
        return RestBuilders.successBuilder().success(linkappSpaceService.updateLinkappSpace(linkappSpace)).build();
    }

    @ApiOperation("删除空间")
    @CommonOperateLogAnnotate(module = LogModule.SPACE, desc = "删除空间")
    @PostMapping("/deleteLinkappSpace")
    public RestMessage deleteLinkappSpace(@RequestBody LinkappSpace linkappSpace) {
        Assert.notNull(linkappSpace.getId(), "空间不能为空");
        //空间下是否绑定设备
        Device device = new Device();
        device.setAreaId(linkappSpace.getId());
        List<Device> deviceList = deviceService.selectDevices(device);
        if (!deviceList.isEmpty()) {
            return RestBuilders.failureBuilder().message("该区域绑定有设备").build();
        }

        //空间下是否绑定设备
        String tenantId = linkappUserContextProducer.getNotNullCurrent().getTenantId();
        QueryWrapper<DeviceRefAreaScope> deviceRefAreaScopeQueryWrapper = new QueryWrapper<>();
        deviceRefAreaScopeQueryWrapper.eq("tenant_id", tenantId);
        deviceRefAreaScopeQueryWrapper.eq("area_id", linkappSpace.getId());
        Integer count = deviceRefAreaScopeService.count(deviceRefAreaScopeQueryWrapper);
        if (count != null && count > 0) {
            return RestBuilders.failureBuilder().message("该区域绑定有设备").build();
        }

        //资产下是否绑定空间
        QueryWrapper<Asset> wrapper = new QueryWrapper();
        wrapper.eq("asset_area_id", linkappSpace.getId());
        List<Asset> assetList = assetService.list(wrapper);
        if (!assetList.isEmpty()) {
            return RestBuilders.failureBuilder().message("该区域绑定有资产").build();
        }

        QueryWrapper qw = new QueryWrapper();
        qw.eq("space_id", linkappSpace.getId());

        Assert.isTrue(calculateConfigMapper.selectCount(qw) == 0, "计算配置引用空间");

        // 逻辑删除字段设为删除标识
//        linkappSpace.setStatus("0");
        return RestBuilders.successBuilder().success(linkappSpaceService.removeById(linkappSpace)).build();
    }

    @ApiOperation("查询空间列表")
    @PostMapping("/selectLinkappSpaceList")
    public RestMessage selectLinkappSpaceList(@RequestBody LinkappSpace linkappSpace) {
        return RestBuilders.successBuilder().data(linkappSpaceService.selectLinkappSpaceList(linkappSpace)).build();
    }

    @ApiOperation("查询空间-单个")
    @PostMapping("/selectLinkappSpace")
    public RestMessage selectLinkappSpace(@RequestBody LinkappSpace linkappSpace) {
        return RestBuilders.successBuilder().data(linkappSpaceService.selectLinkappSpace(linkappSpace)).build();
    }
}
